package com.ruoyi.web.controller.system;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.FastDFSUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TFile;
import com.ruoyi.system.service.ITFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理Controller
 *
 * @author Bandit
 * @date 2022-11-06
 */
@RestController
@RequestMapping("/file/file")
public class TFileController extends BaseController
{
    @Autowired
    private ITFileService tFileService;

    @Autowired
    private FastDFSUtil fastDFSUtil;

    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('file:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFile tFile)
    {
        startPage();
        List<TFile> list = tFileService.selectTFileList(tFile);
        return getDataTable(list);
    }

    /**
     * 导出文件列表
     */
    @PreAuthorize("@ss.hasPermi('file:file:export')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TFile tFile)
    {
        List<TFile> list = tFileService.selectTFileList(tFile);
        ExcelUtil<TFile> util = new ExcelUtil<TFile>(TFile.class);
        util.exportExcel(response, list, "文件管理数据");
    }

    /**
     * 获取文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tFileService.selectTFileById(id));
    }

    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermi('file:file:add')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(MultipartFile file, String md5) throws Exception {
        String url = tFileService.uploadFile(file, md5);
        return AjaxResult.success().put("url", url);
    }

    /**
     * 获取文件MD5
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/md5")
    public AjaxResult getFileMD5(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("文件为空");
        }
        String fileMD5 = tFileService.getFileMD5(file);
        return AjaxResult.success(fileMD5);
    }

    @GetMapping("/baseUrl")
    public AjaxResult getBaseUrl() {
        String url = fastDFSUtil.getBaseUrl();
        return AjaxResult.success(url);
    }

    @PreAuthorize("@ss.hasPermi('file:file:add')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PutMapping("/slices")
    public AjaxResult uploadFileBySlices(MultipartFile slice,
                                         String fileMd5,
                                         Integer sliceNo,
                                         Integer totalSliceNo) throws Exception {
        String filePath = tFileService.uploadFileBySlices(slice, fileMd5, sliceNo, totalSliceNo);
        return AjaxResult.success(filePath);
    }



    /**
     * 修改文件管理
     */
    @PreAuthorize("@ss.hasPermi('file:file:edit')")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFile tFile)
    {
        return toAjax(tFileService.updateTFile(tFile));
    }

    /**
     * 删除文件管理
     */
    @PreAuthorize("@ss.hasPermi('file:file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tFileService.deleteTFileByIds(ids));
    }
}
